<template>
  <div class="register-page">
    <div class="register">
      <div class="login__title">{{ $t('title') }}</div>
      <div class="login__subtitle">{{ $t('jobSeekerRegisterTitle') }}</div>
      <el-form :model="registerForm" :rules="rules" ref="refRegisterForm" @keyup.enter.native="onRegister" size="medium" :label-width="labelWidth">
        <el-row>
          <el-col :span="8">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="registerForm.username" placeholder="请输入用户名" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="registerForm.name" placeholder="请输入姓名" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="registerForm.gender" placeholder="请选择性别">
                <el-option label="男" :value="1"></el-option>
                <el-option label="女" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="密码" prop="password">
              <el-input v-model="registerForm.password" placeholder="请输入密码" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="确认密码" prop="password2">
              <el-input v-model="registerForm.password2" placeholder="请输入确认密码" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期" prop="birthDate">
              <el-date-picker
                  v-model="registerForm.birthDate" type="date" placeholder="出生日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="registerForm.contactPhone" placeholder="请输入联系电话" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="registerForm.email" placeholder="请输入邮箱" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="省/市/区">
              <el-cascader
                  v-model="provinceCityArea"
                  :options="provinceCityAreaOptions"
                  :props="{ expandTrigger: 'hover', value: 'code', label: 'name' }"
                  @change="handleChange"></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="家庭地址" prop="address">
              <el-input v-model="registerForm.address" placeholder="请输入家庭地址" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="verificationCode" label="验证码">
              <el-input v-model="registerForm.verificationCode" placeholder="请输入验证码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <canvas id="captchaCanvas" width="150" height="50" @click="drawCaptcha"></canvas>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" :offset="6">
            <el-button type="primary" @click="onRegister" :loading="loading">注册</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack" :loading="loading">返回登录</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="footer">
      <span>版权所有 &copy; 2024 - 2026 <a href="http://www.52ejn.com" target="_blank">源码学习网</a></span><a href="http://beian.miit.gov.cn" target="_blank">粤ICP备2024308896号-1</a>
    </div>
  </div>
</template>

<script>
import captchaMixin from "@/mixins/captchaMixin";
import areas from "@/locales/area.json";
import candidateApi from "@/api/candidateApi";

export default {
  name: 'TeacherRegister',
  components: {},
  mixins: [
    captchaMixin
  ],
  data () {
    return {
      registerForm: {
        username: '',
        name: '',
        password: '',
        password2: '',
        gender: '',
        birthDate: '',
        contactPhone: '',
        email: '',
        province: '',
        city: '',
        area: '',
        address: '',
        verificationCode: ''
      },
      labelWidth: '80px',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '请确认密码', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请输入性别', trigger: 'blur' }
        ],
        birthDate: [
          { required: true, message: '请输入出生日期', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        verificationCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      loading: false,
      provinceCityArea: [],
      provinceCityAreaOptions: areas.provinces
    }
  },
  created () {
  },
  methods: {
    onReset () {
      this.registerForm = {
        facultyId: '',
        facultyName: '',
        name: '',
        password: '',
        password2: '',
        title: '',
        gender: 1,
        contactPhone: '',
        email: '',
        profile: '',
        verificationCode: ''
      }
    },
    onRegister () {
      this.$refs.refRegisterForm.validate(async (valid) => {
        if (valid) {
          if (this.registerForm.password !== this.registerForm.password2) {
            this.$message.error('两次输入的密码不一致，请重新输入')
            this.registerForm.password = ''
            this.registerForm.password2 = ''
            this.refreshCaptcha()
            return
          }
          if (this.generatedVerificationCode.toLowerCase() !== this.registerForm.verificationCode.toLowerCase()) {
            this.$message.error('验证码错误')
            this.refreshCaptcha()
            return
          }
          this.loading = true
          // 登录验证
          candidateApi.save(this.registerForm)
              .then(data => {
                if (data.code === 1) {
                  this.$message.success('注册成功')
                  this.$router.push({ path: '/Login', query: {type: 3} })
                } else {
                  this.$message.error('注册失败:' + (data.msg ? data.msg : ''))
                  this.refreshCaptcha()
                  this.loading = false
                }
              })
              .catch(error => {
                console.log('注册失败', error)
                this.$message.error('注册失败')
                this.refreshCaptcha()
                this.loading = false
              })
        } else {
          this.refreshCaptcha()
          return false
        }
      })
    },
    refreshCaptcha () {
      this.registerForm.verificationCode = ''
      this.drawCaptcha()
    },
    handleChange(value) {
      if (value && value.length) {
        this.registerForm.province = value[0]
      }
      if (value && value.length > 1) {
        this.registerForm.city = value[1]
      }
      if (value && value.length > 2) {
        this.registerForm.area = value[2]
      }
    },
    onBack () {
      this.$router.push({ name: 'Login', query: {type: 3}})
    }
  }
}
</script>

<style lang="scss">
@import "@/assets/styles/register";
</style>
